package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Button;
import com.codeoss.framework.view.component.Component;

@Service
public class ButtonRenderer extends CommonComponentRenderer {
	@Autowired
	private IScriptServiceStore ScriptServiceStore;
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		Button button = (Button) component;
		viewSerializeMap.put("colorType", button.getColorType());
		String confirmMessage=button.getConfirmMessage();
		if(!StringUtils.isEmpty(confirmMessage)) {
			viewSerializeMap.put("confirmMessage", confirmMessage);
		}
		String clientPageCode=button.getClientPageCode();
		if(!StringUtils.isEmpty(clientPageCode)) {
			viewSerializeMap.put("clientPageCode", clientPageCode);
		}
		Expression labelExpression = button.getLabelExpression();
		if (labelExpression != null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(labelExpression);
			String label = (String)scriptService.eval(labelExpression.getScript(), yunContext.getVarValueMap());
			viewSerializeMap.put("label", label);
		}
		viewSerializeMap.put("model", yunContext.getMasterModel());
		
		String serverPageCode=button.getServerPageCode();
		if(!StringUtils.isEmpty(serverPageCode)) {
			viewSerializeMap.put("serverPageCode", serverPageCode);
		}
		viewSerializeMap.put("block", button.isBlock());
		String para1=button.getPara1();
		if(!StringUtils.isEmpty(para1)) {
			viewSerializeMap.put("para1", para1);
		}
		String para2=button.getPara2();
		if(!StringUtils.isEmpty(para2)) {
			viewSerializeMap.put("para2", para2);
		}
	}
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		Button button = (Button) component;
		Expression visibleExpression = button.getVisibleExpression();
		if (visibleExpression != null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(visibleExpression);
			Object value = scriptService.eval(visibleExpression.getScript(), yunContext.getVarValueMap());
			propSerializeMap.put("visible", value);
		}
		String clientPageCode=button.getClientPageCode();
		if(!StringUtils.isEmpty(clientPageCode)) {
			Object idValue=yunContext.getCurrentId();
			propSerializeMap.put("currentId", idValue);	
		}
		return propSerializeMap;
	}
}
